﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.HeThong
{
    public partial class FormQuanLyTaiKhoan : DevComponents.DotNetBar.RibbonForm
    {
        public FormQuanLyTaiKhoan()
        {
            InitializeComponent();
        }
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        //private AppAccContext db = new AppAccContext();
        private int iduser = 0;
        private void FormQuanLyTaiKhoan_Load(object sender, EventArgs e)
        {
            GetListUser();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            Resert();
        }

        private void bttLuu_Click(object sender, EventArgs e)
        {
            if (iduser == 0)
            {
                Create();
            }
            else
            {
                Edit();
            }
        }
        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }
        private void dgvUserAccount_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowindex = e.RowIndex;
            var row = dgvUserAccount.Rows[rowindex];
            iduser = Convert.ToInt32(row.Cells[0].Value);
            var f = db.UserAccounts.Find(iduser);
            txtMatKhau.Text = f.PassWord;
            txtTenDayDu.Text = f.FullName;
            txtTenTaiKhoan.Text = f.UserName;
            txtXacNhanMatKhau.Text = f.PassWord;
            cbbQuyenHan.SelectedValue = f.PhanQuyen;
            cbkKhoaTaiKhoan.Checked = f.Active;
            KiemTra();
        }

        private void GetListUser()
        {
            dgvUserAccount.DataSource = db.UserAccounts.ToList();
            Resert();
        }

        void Create()
        {
            var u = new UserAccount();
            u.FullName = txtTenDayDu.Text;
            u.ThongTinDonViKeToanId = 1;
            if (CheckUserName() == true)
            {
                MessageBox.Show("Tên tài khoản đã tồn tại!");
                return;
            }
            u.UserName = txtTenTaiKhoan.Text;
            if (txtMatKhau.Text != txtXacNhanMatKhau.Text)
            {
                MessageBox.Show("Xác nhận mật khẩu không đúng!");
                txtXacNhanMatKhau.Text = "";
                txtMatKhau.Text = "";
                return;
            }
            u.PassWord = txtMatKhau.Text;
            u.Active = cbkKhoaTaiKhoan.Checked;
            u.DateTimeRegister = DateTime.Now;
            try
            {
                db.UserAccounts.Add(u);
                db.SaveChanges();
                MessageBox.Show("Thêm mới tài khoản thành công!");
                Resert();
            }
            catch (Exception e)
            {
                MessageBox.Show("Thêm mới tài khoản thất bại!");
                //MessageBox.Show(e.Message);
            }

        }

        void Edit()
        {
            var u = db.UserAccounts.Find(iduser);
            u.FullName = txtTenDayDu.Text;
            u.ThongTinDonViKeToanId = 1;
            if (CheckUserName() == true)
            {
                MessageBox.Show("Tên tài khoản đã tồn tại!");
                return;
            }
            u.UserName = txtTenTaiKhoan.Text;
            if (txtMatKhau.Text != txtXacNhanMatKhau.Text)
            {
                MessageBox.Show("Xác nhận mật khẩu không đúng!");
                txtXacNhanMatKhau.Text = "";
                txtMatKhau.Text = "";
                return;
            }
            u.PassWord = txtMatKhau.Text;
            u.Active = cbkKhoaTaiKhoan.Checked;
            u.DateTimeRegister = u.DateTimeRegister;
            try
            {
                db.Entry(u).State = EntityState.Modified;
                db.SaveChanges();
                Resert();
            }
            catch (Exception e)
            {
                MessageBox.Show("Cập nhập không thành công!");
                //MessageBox.Show(e.Message);
            }
        }

        void Delete()
        {
            var t = db.UserAccounts.Find(iduser);
            if (MessageBox.Show("Bạn muốn xóa tài khoản: " + t.UserName + "!", "Xác nhận", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question) != DialogResult.Yes) return;
            try
            {
                db.UserAccounts.Remove(t);
                db.SaveChanges();
                Resert();
            }
            catch (Exception e)
            {
                MessageBox.Show("Xóa không thành công!");
                MessageBox.Show(e.Message);
            }
        }

        void Resert()
        {
            txtMatKhau.Text = "";
            txtTenDayDu.Text = "";
            txtTenTaiKhoan.Text = "";
            txtXacNhanMatKhau.Text = "";
            cbbQuyenHan.SelectedIndex = -1;
            iduser = 0;
            bttLuu.Enabled = false;
            bttXoa.Enabled = false;
            GetListUser();
        }

        bool CheckUserName()
        {
            var f = db.UserAccounts.Where(t => t.UserName == txtTenTaiKhoan.Text).ToList();
            if (f.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        void KiemTra()
        {
            if (iduser!=0)
            {
                bttLuu.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
                bttXoa.Enabled = false;
            }
        }
        
    }
}